******************************************************************DETAILS
/* 	This do-file creates the data set "dempref.dta" which is used in the analysis
	of Klingelhoefer and Rahat (2026) - Which Democracy Do Political Parties Want? The Role of Party Personalism. */ 

	
******************************************************************A. preliminaries 	
* Create a folder named "Klingelhoefer Rahat 2026 replication" and set as global macro and working directory; here exemplarily:
global file = "C:\Users\abc\Desktop\Klingelhoefer Rahat 2026 replication"
cd "$file"
pwd

* Create a Folder called "_orig" within "Klingelhoefer Rahat 2026 replication"

/* download and place the following files in folders within "_orig" named accordingly:
	"PPDB_v4"		 			https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/0JVUM8
	"PartyFacts_2023" 			https://doi.org/10.7910/DVN/TJINLQ
	"V-Party_Stata_v2" 			https://doi.org/10.23696/vpartydsv2 (choose Country-Party-Date Stata version) */


******************************************************************B. preparation PPDB
* load PPDB data (see beginning; https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/0JVUM8)
use "$file\_orig\PPDB_v4\PPDB_Round2_v4.dta", replace

* drop undemocratic countries (according to Freedom House in 2017/2018)
drop if COUNTRY == "Burkina Faso" | COUNTRY == "Colombia" | COUNTRY == "Ecuador" ///
	| COUNTRY == "Kenya" | COUNTRY == "Malawi" | COUNTRY == "Mexico" ///
	| COUNTRY == "Nigeria" | COUNTRY == "Tanzania" | COUNTRY == "Uganda" ///
	| COUNTRY == "Zambia" | COUNTRY == "Zimbabwe" 
	
* add labels
numlabel _all, add

**************************B.1 recreate leader index (Rahat 2024) 
* generate leader index
generate leader = 0

* party leader play a role in suggesting/proposing candidates for party consideration
tab B25BCANSELA, missing
replace leader = leader + 1 if B25BCANSELA == 1

* party leader(s) play a role in screening/filtering candidates
tab B25BCANSELB, missing
replace leader = leader + 2 if B25BCANSELB == 1

* party leader(s) play a role in selecting/deciding on candidates?	
tab B25BCANSELC, missing
replace leader = leader + 2 if B25BCANSELC == 1

* party leader(s) have the opportunity to veto candidates (post-selection)?
tab B25BCANSELD, missing
replace leader = leader + 1 if B25BCANSELD == 1

* party statutes give the party leader a say in the selection of the deputy party leader(s)
tab C11DEPUTY, missing
replace leader = leader + 1 if C11DEPUTY == 1

* party statutes give the party leader the right to attend all meetings of the national party executive
tab C13LDREXC, missing
replace leader = leader + 1 if C13LDREXC == 1

* party statutes give the party leader the right to attend the national party congress
tab C14LDRCON, missing
replace leader = leader + 1 if C14LDRCON == 1

* party statutes give the party leader the right to summon party officials
tab C15LDRSUM1, missing
replace leader = leader + 1 if C15LDRSUM1 == 1

* party statutes give the party leader the right to summon the party congress
tab C16LDRSUM2, missing
replace leader = leader + 1 if C16LDRSUM2 == 1 

* party statutes explicitly mention that the party leader represents the party externally
tab C17LDRROLE1, missing
replace leader = leader + 1 if C17LDRROLE1 == 1

* party statutes explicitly mention that the national party can only enter coalition agreements with the consent of the party leader
tab C19LDRROLE3, missing
replace leader = leader + 1 if C19LDRROLE3 == 1

* party statutes explicitly give the party leader responsibility for determining the party’s election programme
tab C20LDRROLE4, missing
replace leader = leader + 1 if C20LDRROLE4 == 1

* Role of the party political leader and/or a drafting committee directly appointed by him/her most recent manifesto
tab C101MAN2, missing
replace leader = leader + 1 if C101MAN2 == 1 | C101MAN2 == 2 | C101MAN2 == 5
replace leader = leader + 2 if C101MAN2 == 3

* inspect
tab leader
	
**************************B.2 generate network index
* generate network index
generate network = 0

* candidate selection (legislative elections): individual members suggest (1 point)
tab B22CANSELA, missing
replace network = network + 1 if B22CANSELA == 1

* candidate selection (legislative elections): individual members screen/filter (2 points)
tab B22CANSELB, missing
replace network = network + 2 if B22CANSELB == 1

* candidate selection (legislative elections): individual members selecting / deciding (2 points)
tab B22CANSELC, missing
replace network = network + 2 if B22CANSELC == 1

* candidate selection (legislative elections): individual members veto (1 points)
tab B22CANSELD, missing
replace network = network + 1 if B22CANSELD == 1

* candidate selection (legislative elections): non-members suggest (1 point)
tab B27CANSELA, missing
replace network = network + 1 if B27CANSELA == 1

* candidate selection (legislative elections): non-members screen/filter (2 points)
tab B27CANSELB, missing
replace network = network + 2 if B27CANSELB == 1

* candidate selection (legislative elections): non-members selecting / deciding (2 points)
tab B27CANSELC, missing
replace network = network + 2 if B27CANSELC == 1

* candidate selection (legislative elections): non-members veto (1 points)
tab B27CANSELD, missing
replace network = network + 1 if B27CANSELD == 1

* US legislative candidate selection coded missing, we recode and add 4 points for them (members and non-mebers deciding)
replace network = network + 4 if COUNTRY == "United States"

* leadership selection: individual members suggest (1 point)
tab C40PLSELA, missing
replace network = network + 1 if C40PLSELA == 1

* leadership selection: individual members screen/filter (2 points)
tab C40PLSELB, missing
replace network = network + 2 if C40PLSELB == 1

* leadership selection: individual members selecting / deciding (2 points)
tab C40PLSELC, missing
replace network = network + 2 if C40PLSELC == 1

* leadership selection: individual members veto (1 points)
tab C40PLSELD, missing
replace network = network + 1 if C40PLSELD == 1

* leadership selection: non-members suggest (1 point)
tab C45PLSELA, missing
replace network = network + 1 if C45PLSELA == 1

* leadership selection: non-members screen/filter (2 points)
tab C45PLSELB, missing
replace network = network + 2 if C45PLSELB == 1

* leadership selection: non-members selecting / deciding (2 points)
tab C45PLSELC, missing
replace network = network + 2 if C45PLSELC == 1

* leadership selection: non-members veto (1 points)
tab C45PLSELD, missing
replace network = network + 1 if C45PLSELD == 1

* US presidential candidate, we recode and add 4 points for them (members and non-mebers deciding)
replace network = network + 4 if COUNTRY == "United States"

* inspect
tab network

**************************B.3 generate movement index
* generate movement index
gen movement = network

* any member or non-member can join the party congress
tab A57CCONDEL, missing
replace movement = movement + 1 if A57CCONDEL == 1 | A57CCONDEL == 2

* possibility to hold a referendum on policy issue is in the party statues
tab C1REF1, missing
replace movement = movement + 1 if C1REF1 == 1

* possibility to call for such a referendum: members
tab C7REF7, missing
replace movement = movement + 1 if C7REF7 == 1

* eligibility to vote in such a referendum
tab C8REF8, missing
replace movement = movement + 1 if C8REF8 == 1 | C8REF8 == 2 ///
	| C8REF8 == 3 | C8REF8 == 4

* separate selection procedures for political and electoral leaders
tab C60ELSEP, missing
replace movement = movement + 1 if C60ELSEP == 1

* has party held a policy referendum within the past 5 years
tab C107REF1, missing
replace movement = movement + 1 if C107REF1 == 1

* such a referendum was binding rather than consultative
tab C109REF3, missing
replace movement = movement + 1 if C109REF3 == 1

* participation within the referendum required less than 4 weeks being a party member
tab C110REF4, missing
replace movement = movement + 1 if C110REF4 == 1 | C110REF4 == 2

* role of members in manifesto drafting: formal and/or vote
tab C105MAN6, missing
replace movement = movement + 1 if C105MAN6 == 1 | C105MAN6 == 2 |  C105MAN6 == 3

* role of non-members in manifesto drafting: formal and/or vote
tab C106MAN7, missing
replace movement = movement + 1 if C106MAN7 == 1 | C106MAN7 == 2 |  C106MAN7 == 3

* all members in attendance eligible to vote in party convention
tab C121CONVOT3, missing
replace movement = movement + 1 if C121CONVOT3 == 1

* all members in attendance or not eligible to vote in party convention
tab C122CONVOT4, missing
replace movement = movement + 1 if C122CONVOT4 == 1

* all who attended eligible to vote in convention, whether member or not
tab C123CONVOT5, missing
replace movement = movement + 1 if C123CONVOT5 == 1

* give an additional point for each party that has more than one leader; except US Reps (Trump de-facto leader)
list C10LDRNAME PTYID COUNTRY 
replace movement = movement +1 if PTYID == 3009 | PTYID == 9004 ///
	| PTYID == 9005 | PTYID == 9008 | PTYID == 10003 | PTYID == 15008 ///
	| PTYID == 18008 | PTYID == 19003 | PTYID == 20006  ///
	| PTYID == 11008 | PTYID == 11006

* inspect
tab movement

* drop Latvia and Slovakia (almost all missing values on the variables used to create indices)
drop if COUNTRY == "Latvia" | COUNTRY == "Slovakia"


******************************************************************C. Merge partyfacts
* keep only variables of interest
keep TITLE WAVE VERSION DATE CODER COUNTRY CTRYID YEAR PTYNAME PTYID ///
	PARTYFAMNEW PARTYFAMTXT CR2PTYFED EUROPARTY CR1EPG SYSTEM CR3GOVTPARL ///
	CR3GOVTPRES CR4SEATS1 CR5SEATS2 leader network movement

* save PPDB update
save dempref.dta, replace

* load PartyFacts data (see beginning; https://doi.org/10.7910/DVN/TJINLQ)
insheet using "$file\_orig\PartyFacts_2023\external-parties.csv", clear
keep if dataset_key == "ppdb"

* destring identifier in PPDB
destring dataset_party_id, generate(PTYID)

* keep only relevant variables
keep country name_english partyfacts_id PTYID

* rename variables
rename country pf_country
rename name_english pf_nameenglish

* merge PPDB update with partyfacts
merge 1:1 PTYID using dempref.dta

* drop unmerged, those only existing in PartyFacts but not in PPDB
list pf_country COUNTRY pf_nameenglish PTYNAME PTYID partyfacts_id if _merge == 1
drop if _merge == 1

* double check merge against V-Party (see below)
list pf_country COUNTRY pf_nameenglish PTYNAME PTYID partyfacts_id if _merge == 3

* replace partyfacts ID who are not up to date (_merge == 3)
replace partyfacts_id = 486 if partyfacts_id == 143
replace partyfacts_id = 285 if partyfacts_id == 1743
replace partyfacts_id = 622 if partyfacts_id == 604
replace partyfacts_id = 4 if partyfacts_id == 2507
replace partyfacts_id = 615 if partyfacts_id == 1436
replace partyfacts_id = 1075 if partyfacts_id == 1491
replace partyfacts_id = 7912 if partyfacts_id == 421
replace partyfacts_id = 1338 if partyfacts_id == 586

* check unmerged (only existing in PPDB) against V-Party
list pf_country COUNTRY pf_nameenglish PTYNAME PTYID partyfacts_id if _merge == 2

* add partyfacts ID used in V-Party
replace partyfacts_id = 	1970	 if PTYID == 	2007
replace partyfacts_id = 	1753	 if PTYID == 	3015
replace partyfacts_id = 	2141	 if PTYID == 	5006
replace partyfacts_id = 	901	 	 if PTYID == 	7001
replace partyfacts_id = 	1689	 if PTYID == 	7002
replace partyfacts_id = 	495	 	 if PTYID == 	7003
replace partyfacts_id = 	1303	 if PTYID == 	7004
replace partyfacts_id = 	479		 if PTYID == 	7005
replace partyfacts_id = 	1044	 if PTYID == 	7006
replace partyfacts_id = 	1229	 if PTYID == 	7007
replace partyfacts_id = 	703		 if PTYID == 	7008
replace partyfacts_id = 	5857	 if PTYID == 	8005
replace partyfacts_id = 	1976	 if PTYID == 	9008
replace partyfacts_id = 	469	 	 if PTYID == 	10006
replace partyfacts_id =		3677	 if PTYID ==	12010 
replace partyfacts_id = 	3128	 if PTYID == 	12012
replace partyfacts_id = 	3273	 if PTYID == 	12013
replace partyfacts_id = 	3127	 if PTYID == 	12014
replace partyfacts_id = 	8058	 if PTYID == 	13007
replace partyfacts_id = 	2046	 if PTYID == 	13008
replace partyfacts_id = 	2280	 if PTYID == 	13009
replace partyfacts_id = 	4631	 if PTYID == 	16007
replace partyfacts_id = 	4630	 if PTYID == 	16008
replace partyfacts_id = 	284		 if PTYID == 	17005
replace partyfacts_id = 	3203	 if PTYID == 	18009
replace partyfacts_id = 	3217	 if PTYID == 	18010
replace partyfacts_id = 	848	     if PTYID == 	18011
replace partyfacts_id = 	6561	 if PTYID == 	21002
replace partyfacts_id = 	7655	 if PTYID == 	21003
replace partyfacts_id = 	1746	 if PTYID == 	22001
replace partyfacts_id = 	1804	 if PTYID == 	22002
replace partyfacts_id = 	1515	 if PTYID == 	22003
replace partyfacts_id = 	736	 	 if PTYID == 	22004
replace partyfacts_id = 	2080	 if PTYID == 	22006
replace partyfacts_id = 	6135	 if PTYID == 	22007
replace partyfacts_id = 	6134	 if PTYID == 	22008
replace partyfacts_id = 	654	 	 if PTYID == 	23001
replace partyfacts_id = 	225	     if PTYID == 	23002
replace partyfacts_id = 	356	     if PTYID == 	23003
replace partyfacts_id = 	390	     if PTYID == 	24001
replace partyfacts_id = 	1599	 if PTYID == 	24002
replace partyfacts_id = 	928	     if PTYID == 	24003
replace partyfacts_id = 	54	     if PTYID == 	24004
replace partyfacts_id = 	6	     if PTYID == 	24005
replace partyfacts_id = 	120	     if PTYID == 	26001
replace partyfacts_id = 	481	     if PTYID == 	26004
replace partyfacts_id = 	1105	 if PTYID == 	26006
replace partyfacts_id = 	5662	 if PTYID == 	26008
replace partyfacts_id = 	5969	 if PTYID == 	26009
replace partyfacts_id = 	757	     if PTYID == 	27001
replace partyfacts_id = 	760	     if PTYID == 	27002
replace partyfacts_id = 	2057	 if PTYID ==	27003
replace partyfacts_id = 	982	     if PTYID == 	27004
replace partyfacts_id = 	4865	 if PTYID == 	29001
replace partyfacts_id = 	3918	 if PTYID == 	29003
replace partyfacts_id = 	72	     if PTYID == 	29004
replace partyfacts_id = 	799	     if PTYID == 	29005
replace partyfacts_id = 	99999	 if PTYID == 	29006
replace partyfacts_id = 	513	     if PTYID == 	29007
replace partyfacts_id = 	2522	 if PTYID == 	29008
replace partyfacts_id = 	432	     if PTYID == 	30001
replace partyfacts_id = 	809	     if PTYID == 	30002
replace partyfacts_id = 	301	     if PTYID == 	31001
replace partyfacts_id = 	794	     if PTYID == 	31002
replace partyfacts_id = 	1468	 if PTYID == 	31003
replace partyfacts_id = 	48	     if PTYID == 	31004
replace partyfacts_id = 	1651	 if PTYID == 	31006
replace partyfacts_id = 	1219	 if PTYID == 	33001
replace partyfacts_id = 	1038	 if PTYID == 	33002
replace partyfacts_id = 	4548	 if PTYID == 	33003
replace partyfacts_id = 	1112	 if PTYID == 	34001
replace partyfacts_id = 	1596	 if PTYID == 	34002
replace partyfacts_id = 	3665	 if PTYID == 	34003
replace partyfacts_id = 	5935	 if PTYID == 	34004
replace partyfacts_id = 	514	     if PTYID == 	35001
replace partyfacts_id = 	4218	 if PTYID == 	35002
replace partyfacts_id = 	529	     if PTYID == 	35003
replace partyfacts_id = 	6646	 if PTYID == 	35004
replace partyfacts_id = 	4214	 if PTYID == 	35005
replace partyfacts_id = 	6647	 if PTYID == 	35006
replace partyfacts_id = 	237	     if PTYID == 	37001
replace partyfacts_id = 	1744	 if PTYID == 	37002
replace partyfacts_id = 	556	     if PTYID == 	37003
replace partyfacts_id = 	64	     if PTYID == 	37005
replace partyfacts_id = 	1490	 if PTYID == 	37006
replace partyfacts_id = 	193	 	 if PTYID == 	37007
replace partyfacts_id = 	102	 	 if PTYID == 	37008
replace partyfacts_id = 	821	 	 if PTYID == 	38001
replace partyfacts_id = 	533	 	 if PTYID == 	38002
replace partyfacts_id = 	1150	 if PTYID == 	38003
replace partyfacts_id = 	685	 	 if PTYID == 	38004
replace partyfacts_id = 	5144	 if PTYID == 	38005
replace partyfacts_id = 	3271	 if PTYID == 	38006
replace partyfacts_id = 	2337	 if PTYID == 	39001
replace partyfacts_id = 	2301	 if PTYID == 	39002
replace partyfacts_id = 	1808	 if PTYID == 	42001
replace partyfacts_id = 	360	     if PTYID == 	42002
replace partyfacts_id = 	29	     if PTYID == 	42003
replace partyfacts_id = 	308	     if PTYID == 	42004
replace partyfacts_id = 	999993	 if PTYID == 	43001
replace partyfacts_id = 	999994	 if PTYID == 	43002
replace partyfacts_id = 	999992	 if PTYID == 	43003
replace partyfacts_id = 	999991	 if PTYID == 	43004
replace partyfacts_id = 	2175	 if PTYID == 	43005
replace partyfacts_id = 	2356	 if PTYID == 	45001
replace partyfacts_id = 	2331	 if PTYID == 	45002

* drop those cases without partyfacts_ID
drop if partyfacts_id == .
drop _merge

* save the update
save dempref.dta, replace


******************************************************************C. Merge with V-Party

* load V-Party data (see beginning; https://doi.org/10.23696/vpartydsv2)
use "$file\_orig\V-Party_Stata_v2\V-Dem-CPD-Party-V2.dta", clear

* rename PartyFacts varibale in V-Party
rename pf_party_id partyfacts_id

* replace partyfacts_id for CZ: "Only Option" and "HDZ alliance" same code in original data (3981)
replace partyfacts_id = 99999 if v2paenname == "alliance: The Only Option Coalition"

* replace partyfacts_id that is given in RS: alliances have same code in original data (4011)
replace partyfacts_id = 999991 if v2paenname == "alliance: Serbia is Winning"
replace partyfacts_id = 999992 if v2paenname == "alliance: SPS–JS–ZS"
replace partyfacts_id = 999993 if v2paenname == "alliance: For a Just Serbia"
replace partyfacts_id = 999994 if v2paenname == "alliance: Dveri–DSS"
replace partyfacts_id = 999995 if v2paenname == "alliance: Alliance for a Better Serbia"

* merge V-Party with PPDB incl. partyfacts
merge m:1 partyfacts_id using dempref.dta

* double check merge; those not included in V-Party should be under 5 percent, e.g. 
list COUNTRY YEAR PTYNAME CR5SEATS2 PTYID if _merge == 2
drop if _merge == 2

* double check merge; those not included in PPDB
tab country_name if _merge == 1 & year >= 2012
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Australia"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Austria"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Botswana"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Brazil"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Bulgaria"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Canada"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Chile"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Croatia"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Czech Republic"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Estonia"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "France"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Germany"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Greece"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Hungary"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Ireland"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Israel"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Italy"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Japan"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Lithuania"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Netherlands"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Norway"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Peru"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Poland"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Romania"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Serbia"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "South Korea"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Spain"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Sweden"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "Switzerland"
list v2paenname year partyfacts_id if _merge == 1 & year >= 2012 & country_name == "United Kingdom"

* keep only merged observations
drop if _merge == 1
tab _merge
drop _merge

* keep only years after 2012 inclusive (4 years before the earlierst PPDB coding) 
keep if year >= 2012

* associate CHES data (from 2017 or 2019) with all years (if there was an election in both years, this is the mean)
egen direct = mean(ep_people_vs_elite), by(partyfacts_id)

* drop if there are more than 3 years between the year of V-Party and year of PPDB
gen yeardiff = abs(YEAR - year)
tab yeardiff
list v2pashname YEAR year yeardiff
list v2pashname YEAR year yeardiff if yeardiff > 3
list v2pashname YEAR year yeardiff if v2pashname == "D66"
list v2pashname YEAR year yeardiff if v2pashname == "PASOK"
list v2pashname YEAR year yeardiff if v2pashname == "Hat"
drop if yeardiff > 3

* keep observations with non-missing observations on central DV (consensus)
drop if v2paminor == .

* keep V-Party observations "closest" to the year of data collection of PPDB
egen yeardiff_min = min(yeardiff), by(partyfacts_id)
list YEAR year v2pashname v2paminor yeardiff yeardiff_min if country_name == "Israel"
list YEAR year v2pashname v2paminor yeardiff yeardiff_min if country_name == "Estonia"

keep if yeardiff==yeardiff_min

* if there are multiple non-missing "closest year", keep the newest one
egen helpvar = max(year), by(partyfacts_id)
tab country_name year if helpvar==year

keep if helpvar == year 
list YEAR year v2pashname v2paminor yeardiff yeardiff_min if country_name == "Estonia"

* final cross checks
sort partyfacts_id
list partyfacts_id country_name COUNTRY year YEAR v2paenname PTYNAME in 1/50
list partyfacts_id country_name COUNTRY year YEAR v2paenname PTYNAME in 51/100
list partyfacts_id country_name COUNTRY year YEAR v2paenname PTYNAME in 101/150
list partyfacts_id country_name COUNTRY year YEAR v2paenname PTYNAME in 151/194


******************************************************************D. Remainder

* generate a partyfamily variable
recode PARTYFAMNEW (2 4 5 6 = 1) (3 = 2) (8 = 3) (1 7 9 10 11 12 13 = 4), gen(pfam)
lab def l_pfam 1 "Mainstream" 2 "Greens" 3 "RWPP" 4 "Other"
lab val pfam l_pfam
tab PARTYFAMNEW pfam

* rename central variables
rename v2paind personal
rename v2paind_nr personal_nr
rename v2paplur liberalism
rename v2paplur_mean liberalism_mean
rename v2paplur_nr liberalism_nr
rename v2paminor consensus
rename v2paminor_nr consensus_nr

* generate a standardized variable for the personal party indicator
tab personal
gen personal_std = (personal + 2.729) / 6.5
tab personal_std

* generate a standardized variable for the leader party indicator
tab leader
gen leader_std = leader / 14
tab leader_std

* generate a standardized variable for the nertwork party indicator
tab network
gen network_std = network / 14
tab network_std

* generate a standardized variable for the movement party indicator
tab movement
gen movement_std = movement / 20
tab movement_std

* keep only variables of interest
keep COUNTRY PTYNAME YEAR country_name country_id v2paenname year ///
	direct liberalism liberalism_mean liberalism_nr consensus consensus_nr ///
	personal personal_std personal_nr leader leader_std network network_std movement movement_std ///
	pfam

order COUNTRY PTYNAME YEAR country_name country_id v2paenname year ///
	direct liberalism liberalism_mean liberalism_nr consensus consensus_nr ///
	personal personal_std personal_nr leader leader_std network network_std movement movement_std ///
	pfam

* inspect key variables
sort direct
list COUNTRY PTYNAME YEAR direct

sort liberalism
list COUNTRY PTYNAME YEAR liberalism

sort consensus
list COUNTRY PTYNAME YEAR consensus

sort personal
list COUNTRY PTYNAME YEAR personal

sort leader
list COUNTRY PTYNAME YEAR leader

sort network
list COUNTRY PTYNAME YEAR network

sort movement
list COUNTRY PTYNAME YEAR movement

* sort
sort COUNTRY PTYNAME

* save
save dempref.dta, replace

* print Excel for Appendix lists of party type values
keep COUNTRY PTYNAME YEAR 
export excel using "party list for Appendix Table A1.xls", firstrow(var) replace


